<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4.监听属性:与计算属性的区别是监听属性适合数据刷新次数比较多的情况</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div id="app">
        千米：<input type="text" v-model="kilometers">
        米：{{meters}}
        <br>
        <br>
        <button @click="counter++">点我</button>
        <p>{{counter}}</p>
    </div>
    <p id="info"></p>
    <script>
        var vm = new Vue({
            el: '#app',
            // 数据
            data: {
                kilometers: 0,
                meters: 0,
                counter:0
            },
            // 监听器
            watch: {
                kilometers: function (val) {
                    this.kilometers = val,
                    this.meters = val * 1000
                }
            }
        });
        // 监听器回调方法
        vm.$watch('kilometers', function (newValue, oldValue) {
            document.getElementById('info').innerHTML = '修改前：' + oldValue + '，修改后：' + newValue
            alert('修改前：' + oldValue + '，修改后：' + newValue)
        });
        vm.$watch('counter', function () {
            alert('修改了')
        })
    </script>
</body>

</html>